# -*- coding: utf-8 -*-

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

def view( points, strings ):
    data = ''
    data += 'data.addRows(%d)\n' % len(points)
    for i, point in enumerate(points):
        data += 'data.setValue(%d, 0, \'%s\')\n' % (i, point[0])
        if len(point[1]) == 5:
            sum = point[1][0] + point[1][1] + point[1][2] + point[1][3] + point[1][4]
            if sum == 0:
                continue
            data += 'data.setValue(%d, 1, %d)\n' % (i, point[1][0]*100/sum)
            data += 'data.setValue(%d, 2, %d)\n' % (i, point[1][1]*100/sum)
            data += 'data.setValue(%d, 3, %d)\n' % (i, point[1][2]*100/sum)
            data += 'data.setValue(%d, 4, %d)\n' % (i, point[1][3]*100/sum)
            data += 'data.setValue(%d, 5, %d)\n' % (i, point[1][4]*100/sum)

    response = ''' <h2>View delay distribution</h2>
 <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["linechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', '%s');
        data.addColumn('number', '%% 0 hour delay');
        data.addColumn('number', '%% 1-2 hours delay');
        data.addColumn('number', '%% 3-5 hours delay');
        data.addColumn('number', '%% 6-10 hours delay');
        data.addColumn('number', '%% 10+ hours delay');

        %s

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, {width: 760, height: 480, legend: 'bottom', title: 'Successful orders - %s'});
      }
    </script>
    <div id="chart_div"></div>
    ''' % (strings[1], data, strings[0])
    return response
